package com.example.mylibrarybase.yumengmeng10.view;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.mylibrarybase.R;
import com.example.mylibrarybase.utils.LogUtils;


/**
 * Created by 35876 于萌萌
 * 创建日期: 16:23 . 2016年10月19日
 * 描述:
 * <p>
 * <p>
 * 备注:
 */

public class countdownTextView_10 extends RelativeLayout implements View.OnClickListener{


    private View mInflate;
    private Handler mHandler;
    private Runnable mRunnable;
    private TextView mCountdown_layout_textView;

    public countdownTextView_10(Context context) {
        super(context);
       // setCallBack(context);
    }



    public countdownTextView_10(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);

    }

    public countdownTextView_10(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mInflate = inflater.inflate(R.layout.countdown_layout_10, this);
        mCountdown_layout_textView = (TextView) mInflate.findViewById(R.id.countdown_layout_textView);
        mCountdown_layout_textView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        final int[] cont = {5};
        mCountdown_layout_textView.setText("倒计时"+5);
        mCountdown_layout_textView.setEnabled(false);
        mHandler = new Handler();
        mRunnable = new Runnable() {
            @Override
            public void run() {
                cont[0]--;
                LogUtils.i("倒计时"+cont[0]);
                mCountdown_layout_textView.setText("倒计时"+  cont[0]);
                if (cont[0]>0){
                    mHandler.postDelayed(mRunnable,1000);
                }else{
                    stop();
                }
            }
        };
        mHandler.postDelayed(mRunnable,1000);
    }
    public void stop(){
        mCountdown_layout_textView.setText("发送验证码");
        if (null!=mHandler){
            mHandler.removeCallbacks(mRunnable);
        }
        mCountdown_layout_textView.setEnabled(true);
    }
}
